<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
%>
<jsp:include page="../../import.jsp" flush="true" />
<link rel="StyleSheet" href="<%=path%>/lib/dtree/dtree.css" type="text/css" />
<script type="text/javascript" src="<%=path%>/lib/dtree/dtree.js"></script>
<body>


<SCRIPT LANGUAGE="JavaScript">
<!--
$(function(){
	$("#editDiv").dialog({
		autoOpen: false,
		bgiframe: true,
		height: 700,
		width:600,
		modal: true,
		buttons: {
			"提交": function() { 
				//$(this).dialog("close"); 
				_submit();
			}, 
			"取消": function() { 
				$(this).dialog("close"); 
			} 
		}
	});
});


var roleCache = {};
roleCache['ALL'] = '全部';
<s:iterator value="roles.resultList">   
	roleCache['<s:property value="ID"/>']='<s:property value="NAME"/>';
</s:iterator> 

var toTranslate = function(_rid){
	_rid = _rid.replace(/\|/g,';');
	var tt=[];
	var toWrite = function(id){
		if(id.indexOf(":")==-1){
			tt.push(roleCache[id]);
		}else{
			tt.push(roleCache[id.substring(0,id.indexOf(":"))]);
		}
		
	}
	if(_rid.indexOf(';')>-1){
		var tmp = _rid.split(';');
		for(var i = 0; i<tmp.length;i++){
			toWrite(tmp[i]);
		}
	}else{
		toWrite(_rid);
	}
	return tt.join(';');
}

//-->
</SCRIPT>



<!-- <input type="button" id="add" value="增加新业务" onclick="addNewTask();"/> -->
<!-- <s:property value="#session.QDEVELOP_LOGIN_SESSION_KEY.rid"/> -->
<table class="QueryTable">
<thead>
	<th><A HREF="javascript:goOrder('NAME')">用户角色</A></th>
	<th><A HREF="javascript:goOrder('RELATION')">所能拥有角色</A></th>
	<th>操作[<A HREF="javascript:addNewRelation();">新增角色关系</A>]</th>
</thead>
<!-- even odd -->
 <s:iterator value="rb.resultList" id="m" status="cust_stat">  
 <TR class='<s:if test="#cust_stat.even">anthor_tr</s:if>'>  
		<TD><s:property value="NAME"/></TD>
		<TD><SCRIPT LANGUAGE="JavaScript">document.write(toTranslate('<s:property value="RELATION"/>'));	</SCRIPT></TD>
		<TD>[<A HREF='javascript:editRelation(<s:property value="RID"/>,"<s:property value="NAME"/>","<s:property value="RELATION"/>")'>修改</A>]
		/ [<A HREF='javascript:delRelation(<s:property value="RID"/>,"<s:property value="NAME"/>")'>删除</A>]
		</TD>
</TR>
 </s:iterator> 
</table>
<jsp:include page="../../pageination.jsp" flush="true" />

<div id="editDiv" title="角色关系编辑操作" style="display:none">
	<span align='center' >
		
		<TABLE>
		<TR>
			<TD valign='top' >
			<FORM METHOD='POST' ACTION='<%=path%>/security/roleRelationUpdate.action' onSubmit="return checkData();">
				<INPUT TYPE="hidden" NAME="index"   id="index" value=''>
				<INPUT TYPE="hidden" NAME="rid"   id="rid" value=''>
				<INPUT TYPE="hidden" NAME="oid"   id="oid" value=''>
				<INPUT TYPE="hidden" NAME="relation" id="relation"  value='' >
			</FORM>
			<TABLE >
			<TR>
					<TD >用户角色：</TD>
					<TD><!-- onchange="$('#rid').val(this.options[this.selectedIndex].value)" -->
					<SELECT NAME="name" id="name" onchange="$('#rid').val(this.options[this.selectedIndex].value)" >
						<s:iterator value="roles.resultList">   
							<option value='<s:property value="ID"/>'><s:property value="NAME"/></option>
						</s:iterator> 
					</SELECT>
					</TD>
			</TR>
			<TR>
					<TD colspan='2'>
					<div id="relationList"></div>
					</TD>
			</TR>
			</TABLE>
			
			</TD>
			<TD valign='top'>
			<div class="dtree">
			<!-- <INPUT TYPE="checkbox" id="c_ALL" value="ALL">全部 -->
		<SCRIPT LANGUAGE="JavaScript">
			mytree = new dTree('mytree');
			mytree.config.useCheckbox = true; 				
			<s:iterator value="roles.resultList" id="m" status="cust_stat">    
				 mytree.add(<s:property value="ID"/>,<s:property value="PID"/>,'<s:property value="NAME"/>');	
			</s:iterator> 
			document.write(mytree);
		</SCRIPT>
		</div>

			</TD>
		</TR>
		</TABLE>			
	
	</span>
</div>
<!--  <TEXTAREA id="debug" ROWS="50" COLS="100"></TEXTAREA><button value="Debug" onclick="$('#debug').val($('.dtree').html());"/> -->
</body>
<SCRIPT LANGUAGE="JavaScript">


var onclickNode  = function(id){
	alert(id);
}


var _submit = function(){
		if($('#rid').val() == '-1' || $('#rid').val() == ''){
			alert('用户角色必须设定！');
			return;
		}
		$('#editDiv').dialog('close');
		wait.show();
		//$('#rid').val($('#name').val());
		$('#relation').val(getRelationValue());

	$('form:first').submit();
}

var editRelation = function(rid,name,relation){	
	$('#editDiv').dialog('open');
	$('#rid').val(rid);
	$('#oid').val(rid);
	$('#name').val(name);
	$('#relation').val(relation);
	$('#relationList').html('');
	setDtreeValues(relation);
	createRelation(relation);
	$('#index').val('roleRealationMaintainEdit');
	
}

var delRelation = function(rid,name){
	if(confirm('确定角色关系【'+name+'】?')){
		$('#rid').val(rid);
		$('#index').val('roleRealationMaintainDel');
		_submit();
	}
}
var addNewRelation = function(){
//	$('#editDiv input').val('');
	$('#name').val('');
	$('#relationList').html('');
	setDtreeValues(null);
	$('#editDiv').dialog('open');
	$('#index').val("roleRealationMaintainInsert");
}
//=========================================================

var getRelationValue = function(){
	var _tmp = [];
	var _radioCache = {};
	$('#relationList input[name^="readType_"]').each(function(){
		if(this.checked){
			_radioCache[this.name] = this.value;
		}
	});
	var _t;
	$('#relationList div[id^="rl_"]').each(function(){
		_t = this.id.substring(3);
		_v = 'readType_'+_t;
		_tmp.push(_t);
	});
	return _tmp.join('|');
}

$('.dtree input[id^="c_"]').click(function(){
	if(this.checked){
		createRelation(this.value+":0");
	}else{
		removeRelation(this.value+":0");		
	}
});

var removeRelation = function(_rel){
	if(_rel == null || _rel == ':0'){
		return;
	}
	var _remove = function(_val){
		if(_val.indexOf(':')>-1){
			var tmp = _val.split(':');
			$("#rl_"+tmp[0]).remove();
		}else{
			$("#rl_"+_val).remove();
		}
	}
	if(_rel.indexOf(';')>-1){
		var tmp = _rel.split(';');
		for(var i=0;i<tmp.length;i++){
			_remove(tmp[i]);
		}
	}else{
		_remove(_rel);
	}
}

var createRelation = function(_rel){
	//_rel =  _rel.replace(/|/g,);
	if(_rel == null || _rel == ':0'){
		return;
	}
	var _create = function(_val){
		if(_val.indexOf(':')>-1){
			var tmp = _val.split(':');
			$('#relationList').append('<div id=rl_'+tmp[0]+'>'+roleCache[tmp[0]]
				+'<INPUT TYPE="radio" NAME="readType_'+tmp[0]+'" value="0" '+(tmp[1]=='0'?'checked':'')+'>只读 <INPUT TYPE="radio" NAME="readType_'+tmp[0]+'" value="1" '+(tmp[1]=='1'?'checked':'')+'>读写</div>');
		}else{
			$('#relationList').append('<div id=rl_'+_val+'>'+roleCache[_val]
				+'<INPUT TYPE="radio" NAME="readType_'+_val+'" value="0" checked >只读 <INPUT TYPE="radio" NAME="readType_'+_val+'" value="1" >读写</div>');
		}
	}
	if(_rel.indexOf('|')>-1){
		var tmp = _rel.split('|');
		for(var i=0;i<tmp.length;i++){
			_create(tmp[i]);
		}
	}else{
		_create(_rel);
	}
}

var setDtreeValues = function(values){
	$('.dtree input').attr("checked",false);

	if(values!= null && values!=''){
		$('.dtree input').each(function(){
			if(values.indexOf(this.value)>-1 && this.value!=''){
				$(this).attr("checked",true);
			}
		});
	}
}
</SCRIPT>
